package appname.example.com.testmvvm;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;

import appname.example.com.testmvvm.databinding.ItemViewBinding;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

    ArrayList<Testbean> datas = new ArrayList<>();

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ItemViewBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(viewGroup.getContext()),
                R.layout.item_view, viewGroup,
                false);
        return new MyHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        myHolder.setData(datas.get(i));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void setData(ArrayList<Testbean> datas) {
        this.datas = datas;
    }

    class MyHolder extends RecyclerView.ViewHolder {
      public   ItemViewBinding binding;

        public MyHolder(ItemViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void setData(Testbean testbean) {
            binding.setVariable(BR.ctrl,testbean);
        }
    }
}
